package cn.jett.domain.strategy.model.entity;

import cn.jett.types.common.Constants;
import cn.jett.types.exception.AppException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 战斗天使
 * @Description 策略规则实体
 * @Date 2024/6/16
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class StrategyRuleEntity {
    /** 策略ID */
    private Long strategyId;
    /** 奖品ID */
    private Integer awardId;
    /** 抽奖规则类型(1.策略规则 2.奖品规则) */
    private Integer ruleType;
    /** 抽奖规则类型(rule_lock) */
    private String ruleModel;
    /** 抽奖规则比值 */
    private String ruleValue;
    /** 抽奖规则描述 */
    private String ruleDesc;

    /**
     * 获取rule_weight的value
     * @return
     */
    public Map<String, List<Integer>> getWeightValue(){
        //规则不匹配则返回空
        if (!"rule_weight".equals(ruleModel)){
            return null;
        }
        //先对权重按空格分割
        String [] ruleValueGroups = ruleValue.split(Constants.SPACE);
        Map<String, List<Integer>> resultMap = new HashMap<>();
        for (String ruleValueGroup: ruleValueGroups) {
            //检查输入是否为空
            if (null == ruleValueGroup || ruleValueGroup.isEmpty()){
                return resultMap;
            }
            //分割字符串获得键和值
            String[] parts = ruleValueGroup.split(Constants.COLON);
            if (parts.length!=2){
                throw new IllegalArgumentException("rule_weight rule_rule invalid input format" + ruleValueGroup);
            }
            //将每组权重的值分割并加入List集合
            String[] valueStrings = parts[1].split(Constants.SPLIT);
            List<Integer> values = new ArrayList<>();
            for (String valueString:valueStrings) {
                values.add(Integer.parseInt(valueString));
            }
            //将键和值放入map
            resultMap.put(ruleValueGroup, values);
        }
        return resultMap;
    }
}
